package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.common.service.UploadService;
import com.luchao.back.pojo.FriendLinks;
import com.luchao.back.service.FriendLinksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "友情链接管理")
@RestController
@RequestMapping("/flinks")
public class FriendLinksController {

    @Value("${file.upload.path}")
    private String path;

    @Autowired
    private FriendLinksService friendLinksService;

    @Autowired
    private UploadService uploadService;

    @ApiOperation("分页获取友情链接信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getFlinksByPage")
    public ResponseResult getFlinksByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = friendLinksService.getFriendLinksCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<FriendLinks> pageResult = new PageResult<FriendLinks>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(friendLinksService.getFriendLinksByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("上传友情链接图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "flimage", value = "上传的图片文件", required = true, paramType = "query", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/uploadImage")
    public ResponseResult uploadImage(@RequestPart MultipartFile flimage) {
        return uploadService.uploadPhoto(path, flimage) ? ResponseResult.success(ResponseResult.SUCCESS, "上传成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "上传失败", Execute.ERROR);
    }

    @ApiOperation("校验友情链接名称唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "flname", value = "友情链接名称", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkFlinks")
    public ResponseResult checkFlinks(String flname) {
        return friendLinksService.checkFlinks(flname) > 0 ? ResponseResult.success(ResponseResult.FAILED, "链接名称已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "链接名称不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("添加友情链接")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "friendLinks", value = "友情链接信息", required = true, paramType = "formData", dataType = "FriendLinks", dataTypeClass = FriendLinks.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addFlinks")
    public ResponseResult addFlinks(@RequestBody FriendLinks friendLinks) {
        return friendLinksService.addFlinks(friendLinks) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("删除友情链接")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "flid", value = "删除友情链接", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/deleteFlinks")
    public ResponseResult deleteFlinks(Integer flid) {
        return friendLinksService.deleteFlinks(flid) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "删除成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "删除失败", Execute.ERROR);
    }

    @ApiOperation("批量删除友情链接")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "friendLinks", value = "批量删除友情链接", required = true, paramType = "formData", dataType = "List<FriendLinks>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchDeleteFlinks")
    public ResponseResult batchDeleteFlinks(@RequestBody List<FriendLinks> friendLinks) {
        return friendLinksService.batchDeleteFlinks(friendLinks) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "操作失败", Execute.ERROR);
    }
}
